﻿using CodeFirsrtDemo.Entity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CodeFirsrtDemo
{
    class Program
    {
        static void Main(string[] args)
        {

             var context = new DataContext();

            #region"增加"
            //Console.WriteLine("添加记录到学院表中....");


            ////添加记录前执行sql命令去清除所有的记录
            //context.Database.ExecuteSqlCommand("delete departments");

            ////添加一条记录
            //var department1 = new Department()
            //{
            //    Name = "电子信息工程学院",
            //    Dscn = "软件技术专业____"
            //};
            //var department2 = new Department()
            //{
            //    Name = "艺术学院",
            //    Dscn = "广告设计专业____"
            //};
            ////添加此类对象到上下文，保存更改
            //context.Departments.Add(department1);
            //context.Departments.Add(department2);
            //context.SaveChanges();
            //Console.WriteLine("添加成功！");
            #endregion

            //查询 显示所有的二级学院
            var deptList = context.Departments.ToList();
            //var i = 1;
            //foreach (var item in deptList)
            //    Console.WriteLine(i++ + "." + item.Name + " - " + item.Dscn);

            Console.WriteLine("按编号排序");
            deptList = context.Departments.OrderBy(x => x.SortCode).ToList();
            var i = 1;
            foreach (var item in deptList)
                Console.WriteLine(i++ + "." + item.Name + " - " + item.Dscn);



            //修改一条记录
            //var department = context.Departments.SingleOrDefault(x => x.Name == "艺术学院");
            //var department = context.Departments.Find(Guid.Parse("2C0E8532-A343-491B-B9FB-2E5AADDA204F"));
            //department.Dscn = "原艺术设计系,有广告设计专业、服装设计专业";
            //context.SaveChanges();

            //删除一条记录
            //var del=context.Departments.SingleOrDefault(x => x.Name == "艺术学院");
            //context.Departments.Remove(del);
            //context.SaveChanges();
            //Console.ReadKey();
        }
    }
}
